package com.org;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;

import com.org.model.Log;
import com.org.utils.servlet.ServletUtils;
import com.org.utils.sql.JsonHandler;
import com.org.utils.string.SystemConfig;
import com.org.utils.sysconfg.SystemConfigUtil;

@SuppressWarnings("all")
public class BaseController {
	 

	public static final String VIEW = "view";
	public static final String LIST = "list";
	public static final String STATUS = "status";
	public static final String WARN = "warn";
	public static final String SUCCESS = "success";
	public static final String ERROR = "error";
	public static final String MESSAGE = "message";
	public static final String CONTENT = "content";

	/**
	 * @return
	 */
	public SystemConfig getSystemConfig() {
		return SystemConfigUtil.getConfig();
	}
	/**
	 * 
	 * @return
	 * @throws JSONException
	 */
	public Map<String, Object> getRequestParams() throws Exception {
		HttpServletRequest request=ServletUtils.getRequest();
		Integer pageSize=Integer.valueOf(request.getParameter("pageSize"));
		Integer page=Integer.valueOf(request.getParameter("page"));
		
		Map<String, Object> params = new HashMap<String, Object>();
	
		int limit = pageSize;
		int start = ((page <= 0 ? 0 : page - 1)) * limit;
		params.put("start", start);
		params.put("limit", limit);
		String sortname = request.getParameter("sort[0][field]");
		if (sortname != null && !"".equals(sortname.trim()))
			params.put("sortname", sortname);
		String sortorder = request.getParameter("sort[0][dir]");
		if (sortorder != null && !"".equals(sortorder.trim()))
			params.put("sortorder", sortorder);
		String conditionsStr = request.getParameter("conditions");
		JSONArray conditions = null;
		if (conditionsStr != null) {
			conditions = new JSONArray(conditionsStr);
			String temp = JsonHandler.jsonToSQLConditions(conditions);
			if (!temp.isEmpty()) {
				params.put("conditions", temp);
			}
		}
		return params;
	}
	/***
	 * 写入日志操作..
	 * @param request请求对象
	 * @param content操作的内容文本
	 * @param operation操作的动作(Insert,Delete,Update,Select)
	 * @param parameter请求参数
	 * 
	 */
	public Log writerLog(HttpServletRequest request,HttpServletResponse response,String userName,String content,
			String operation,String parameter)throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		Log log=new Log();
		log.setId(null);
		log.setContent(userName+content);
		log.setModifyDate(new Date());
		log.setCreateDate(new Date());
		log.setIp(ServletUtils.getIpAddr(request));
		log.setOperation(operation);//操作动作/(Insert,Delete,Update,Select)
		log.setOperator(userName);//操作人
		log.setParameter(parameter);//操作的请求参数
		return log;
	}
 
	/***
	 * @param content
	 * @param type(text/plain,text/html,text/xml,text/json)
	 * @param response
	 * @return
	 */
	public static String ajax(String content, String type,HttpServletResponse response) {
		try {
			response.setContentType(type + ";charset=UTF-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(content);
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/***
	 * 
	 * @param jsonString
	 * @param response
	 * @return
	 */
	public String ajaxJson(String jsonString,HttpServletResponse response) {
		return ajax(jsonString, "text/json",response);
	}
	
	/**
	 * @param message
	 * @return
	 */
	public static String responseMessage(String message,String type,HttpServletResponse response) {
		return ajax(message, type,response);
	}
	
	public static void main(String[] args) {
		//ajaxJsonErrorMessage("tttt");
	}
	
}
